package com.yenlien.traceyun.breed.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.yenlien.traceyun.breed.dao.BreedCropDao;
import com.yenlien.traceyun.breed.dao.BreedCropDetailDao;
import com.yenlien.traceyun.breed.dao.BreedDao;
import com.yenlien.traceyun.breed.dao.OutDao;
import com.yenlien.traceyun.breed.dao.PreventDao;
import com.yenlien.traceyun.breed.dao.QuarantineDao;
import com.yenlien.traceyun.breed.dao.RecallDao;
import com.yenlien.traceyun.breed.entity.Breed;
import com.yenlien.traceyun.breed.entity.BreedCrop;
import com.yenlien.traceyun.breed.entity.BreedCropDetail;
import com.yenlien.traceyun.breed.entity.Out;
import com.yenlien.traceyun.breed.entity.Prevent;
import com.yenlien.traceyun.breed.entity.Quarantine;
import com.yenlien.traceyun.breed.entity.Recall;
import com.yenlien.traceyun.webase.exception.BusinessException;

@Deprecated
//@Service
public class RecallService {
    @Autowired
    private RecallDao recallDao;
    @Autowired
    private BreedDao breedDao;
    @Autowired
    private BreedCropDao breedCropDao;
    @Autowired
    private BreedCropDetailDao breedCropDetailDao;
    @Autowired
    private PreventDao preventDao;
    @Autowired
    private QuarantineDao quarantineDao;
    @Autowired
    private OutDao outDao;

    public Page<Recall> findByList(final Recall recall) {
        Pageable pageable = PageRequest.of(recall.getPage(), recall.getPageSize(), Sort.Direction.DESC, ("createtime"));
        return null;
//        return recallDao.findAll(new Specification<Recall>() {
//            private static final long serialVersionUID = -1396496630802017658L;
//            @Override
//            public Predicate toPredicate(Root<Recall> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//                Path<String> seedName = root.get("seedName");
//                Path<String> batch = root.get("batch");
//                Path<String> recallUser = root.get("recallUser");
//                //构造查询条件
//                List<Predicate> list = new ArrayList<Predicate>();
//                Predicate[] predicateA = new Predicate[list.size()];
//                Predicate predicateAnd = cb.and(list.toArray(predicateA));
//
//                if (StringUtils.isNotEmpty(recall.getSeedName())) {
//                    List<Predicate> list2 = new ArrayList<>();
//                    list2.add(cb.like(seedName, "%" + recall.getSeedName() + "%"));
//                    list2.add(cb.like(batch, "%" + recall.getBatch() + "%"));
//                    list2.add(cb.like(recallUser, "%" + recall.getRecallUser() + "%"));
//                    Predicate[] predicateO = new Predicate[list2.size()];
//                    Predicate predicateOr = cb.or(list2.toArray(predicateO));
//                    return query.where(predicateAnd, predicateOr).getRestriction();
//                } else {
//                    return predicateAnd;
//                }
//            }
//        }, pageable);
    }

    public void save(Recall recall) {
        String breedBatch = "";
        if (("养殖").equals(recall.getLink())) {
            breedBatch = recall.getBatch();
        } else if (("产出物").equals(recall.getLink())) {
            BreedCrop breedCrop = breedCropDao.findByBatch(recall.getBatch());
            if (breedCrop == null) {
                throw new BusinessException("A0001", "产出物不存在");
            }
            List<BreedCropDetail> list = breedCropDetailDao.findByBreedCropId(breedCrop.getId());
            if (list.size() == 0) {
                throw new BusinessException("A0001", "产出物不存在");
            } else {
                breedBatch = list.get(0).getBatch();
            }
        } else if (("防疫").equals(recall.getLink())) {
            Prevent prevent = preventDao.findByPreventBatch(recall.getBatch());
            if (prevent == null) {
                throw new BusinessException("A0001", "防疫批号不存在");
            }
            breedBatch = prevent.getBatch();
        } else if (("检疫").equals(recall.getLink())) {
            Quarantine quarantine = quarantineDao.findByBatch(recall.getBatch());
            if (quarantine == null) {
                throw new BusinessException("A0001", "检疫批号不存在");
            }
            breedBatch = quarantine.getBreedBatch();
        } else if (("出场").equals(recall.getLink())) {
            Out out = outDao.findByOutBatch(recall.getBatch());
            if (out == null) {
                throw new BusinessException("A0001", "检疫批号不存在");
            }
            breedBatch = out.getBreedBatch();
        }

        Breed breed = breedDao.findByBatch(breedBatch);
        if (breed == null) {
            throw new BusinessException("A0001", "养殖批号不存在");
        }
        if (breed.getIsRecall() == 1) {
            throw new BusinessException("A0001", "养殖批号已召回");
        }
        breed.setIsRecall(1);
        breedDao.save(breed);
    }

    public Recall findById(String id) {
        Recall recall = recallDao.findById(id);
        return recall;
    }
}
